<?php 
/**
 * @version        $Id$
 * @author         master@xuewl.com
 * @copyright      Copyright (c) 2007 - 2014, Chongqing xuewl Information Technology Co., Ltd.
 * @link           http://www.xuewl.com
**/
namespace Member\Controller;
use \Member\Controller\InitController;
class AccountController extends InitController {
	
	public function _initialize() {
		parent::_initialize();
		$this->db = D('Member');
		$this->detail_db = D('Member_detail');
		$this->sms_db = D('Sms_report');
		$this->account_db = D('Member_account');
		$this->cash_db = D('Cash');
		$this->info_db = D('Account_info');
		$this->bean_db = D('Score');
		$this->bank_db = D('Bank_type');
		
	}

	
	public function index() {
	}
	
	/*申请提现初始化页面*/
	public function cash(){
		$info = $this->db->where(array('userid'=>$this->userid))->find();
		$cash=$this->account_db->where(array('userid'=>$this->userid,'pay_type'=>'alipay'))->field('id,username,account,status')->find();
		$cashs=$this->account_db->where(array('userid'=>$this->userid,'pay_type'=>'alipay'))->field('id,username,account,status')->select();
		$cashss = array();
		foreach ($cashs as $key => $v) {
			$cashss[$key]['accountID'] = $v['id'];
			$cashss[$key]['account'] = $v['account'];
			$cashss[$key]['name'] = $v['username'];
		}
		$cashss = json_encode($cashss);
		$setting = getcache('setting', 'member');
		$coin=exchange_money($info['u_coin']);
		$SEO=seo(0,"申请提现");
		//echo json_encode($cash);
		include template('cash');
	}
	
	
	/*绑定支付宝初始化页面*/
	public function alipay(){
		$bind=$this->is_bind();
		$SEO=seo(0,"绑定支付宝");
		if(!$bind){
			$this->error('绑定提现账号需完成手机号绑定，请先绑定手机号！', U('Member/Profile/binding_tel'));
		}
		$alipay=$this->account_db->where(array('userid'=>$this->userid,'pay_type'=>'alipay'))->select();
		include template('alipay');
	}
	
	/*发送支付宝验证码*/
	public function getCode(){
		$sms=A("Member/Sms");
		$sms->code("您的绑定支付宝验证码为：","alipay");
		
	}
	/*绑定支付宝*/
	public function binding_pay(){
		$this->bind('alipay','绑定支付宝成功！');
	}
	
	
	/*编辑支付宝初始化页面*/
	public function pay_edit(){
		$SEO=seo(0,"编辑支付宝信息");
		$id = (int) I('id');
		$user=$this->account_db->where(array('id'=>$id))->find();
		include template('alipay_edit');
	
	}
	
	
	/*编辑支付宝数据*/
	public function alipay_edit(){
		$infos=I('post.');
		$info['username']=$infos['name'];
		$info['account']=$infos['account'];
		$info['code']=$infos['code'];
		$account=$this->check_account($infos['account']);
		if($account){
				$data['flag']=0;
				$data['msg']="该账户已被绑定！";
				echo json_encode($data);
		}else{
		
		$result=$this->account_db->where(array("id"=>$infos['id']))->save($info);
		//echo $this->account_db->getLastSql();
		//var_dump($result);
		if ($result){
				$data['flag']=1;
				$data['msg']="更改成功！";
				echo json_encode($data);
		}
		}
	}
	/*绑定财付通初始化页面*/
	public function ten(){
	   $SEO=seo(0,"绑定财付通");
		$info = $this->db->where(array('userid'=>$this->userid))->find();
		$cash=$this->account_db->where(array('userid'=>$this->userid,'pay_type'=>'tenpay'))->find();
		$cashs=$this->account_db->where(array('userid'=>$this->userid,'pay_type'=>'tenpay'))->select();
		
		$cashss = array();
		foreach ($cashs as $key => $v) {
			$cashss[$key]['accountID'] = $v['id'];
			$cashss[$key]['account'] = $v['account'];
			$cashss[$key]['name'] = $v['username'];
		}
		$cashss = json_encode($cashss);
		$setting = getcache('setting', 'member');
		$coin=exchange_money($info['u_coin']);
		include template('ten');
	}
	
	
	/*绑定财付通用户信息始化页面*/
	public function tenpay(){
		$SEO=seo(0,"绑定财付通信息");
		$alipay=$this->account_db->where(array('userid'=>$this->userid,'pay_type'=>'tenpay'))->select();
		include template('tenpay');
	}
	
	/*财付通验证码*/
	public function tenPay_code(){
		$sms=A("Member/Sms");
		$sms->code("您的绑定财付通验证码为：","tenpay");
		
	}
	
	/*更改财付通信息初始化页面*/
	public function ten_edit(){
		$SEO=seo(0,"更改财付通信息");
		$id = (int) I('id');
		$user=$this->account_db->where(array('id'=>$id))->find();
		include template('ten_edit');
	}
	
	/*绑定财付通*/
	public function binding_ten(){
		$this->bind('tenpay',"绑定财付通成功！");
	}
	
	/*银行验证码*/
	public function bank_code(){
		$this->code("你的银行绑定账户信息验证码为：","bank");

	}
	
	/*绑定银行信息初始化页面*/
	public function bank(){
		$SEO=seo(0,"绑定银行信息");
		$info = $this->db->where(array('userid'=>$this->userid))->find();
		$cash=$this->account_db->where(array('userid'=>$this->userid,'pay_type'=>'bank'))->find();
		$cashs=$this->account_db->where(array('userid'=>$this->userid,'pay_type'=>'bank'))->select();
		$cashss = array();
		foreach ($cashs as $key => $v) {
			$cashss[$key]['accountID'] = $v['id'];
			$cashss[$key]['account'] = $v['account'];
			$cashss[$key]['name'] = $v['username'];
		}
		$cashss = json_encode($cashss);
		$setting = getcache('setting', 'member');
		$coin=exchange_money($info['u_coin']);
		include template('bank');
	}
	
	public function bind_bank(){
		$SEO=seo(0,"绑定银行信息");
		$bank=$this->bank_db->select();
		$account=$this->account_db->where(array('pay_type'=>'bank','userid'=>$this->userid))->select();
		include template('bind_bank');
	}
	
	
	/*银行绑定*/
	public function bank_binding(){
		$info=I('post.');
		$account=$info['account'];
		$name=$info['name'];
		$code=$info['code'];
		$infos['account']=$info['account'];
		$infos['username']=$info['name'];
		$infos['userid']=$this->userid;
		$infos['pay_type']='bank';
		$infos['status']=1;
		$infos['add_time']=time();
		$bank_info=array();
		$bank_info['bank_name'] = $info['userBank1'];
		$bank_info['open_bank']=$info['userBank2'];	
		$info['bank_info']=serialize($bank_info);
		$bind=$this->check_account($info['account']);
		if (!$bind){
		$pay=$this->account_db->add($infos);
		}
		if ($pay){
		$sms=$this->sms_db->where(array('userid'=>$this->userid,'id_code'=>$code))->find();
		if ($sms && $sms['enum']=='bank'){
				$data['flag']=1;
				$data['msg']="绑定成功！";
				echo json_encode($data);
		}else{
			
				$data['flag']=0;
				$data['msg']="绑定失败！";
				echo json_encode($data);
		
		}
		
		}
	}
	
	/*更改银行信息*/
	public function bank_edit(){
		$SEO=seo(0,"更改银行信息");
		$bank=$this->bank_db->select();
		$id = (int) I('id');
		$account=$this->account_db->where(array('id'=>$id))->find();
		extract($account);
		$info=unserialize($bank_info);
		include template('bank_edit');
	}
	
	/*编辑银行信息*/
	public function edit_bank(){
		$infos=I('post.');
		$info=array();
		$info['username']=$infos['name'];
		$info['account']=$infos['account'];
		$code=$infos['code'];
		$bank_info=array();
		$bank_info['bank_name'] = $infos['userBank1'];
		$bank_info['open_bank']=$infos['userBank2'];
		$info['bank_info']=serialize($bank_info);
		$sms_code=$this->sms_db->where(array('id_code'=>$code,'userid'=>$this->userid))->find();
		if($sms_code){
	    $result=$this->account_db->where(array("id"=>$infos['id']))->save($info);
		if($result){
				$data['flag']=1;
				$data['msg']="更改成功！";
				echo json_encode($data);
		}else{
				$data['flag']=0;
				$data['msg']="更改失败！";
				echo json_encode($data);
		}
		}
		
	}
	
	/*支付宝申请提现*/
	public function cash_money(){
		$setting = getcache('setting', 'member');
		$money=$setting['alipay'];
		$this->cash_bind($money);
	}
	/*财付通提现*/
	public function cash_ten(){
		$setting = getcache('setting', 'member');
		$money=$setting['tenpay'];
		$this->cash_bind($money);
		
	}
	
	/*提现银行卡*/
	public function cash_bank(){
		$setting = getcache('setting', 'member');
		$money=$setting['bank'];
		$this->cash_bind($money);
	}
	
	
	/*绑定*/
	public function bind($type,$msg){
		$info=I('post.');
		$account=$info['account'];
		$name=$info['name'];
		$code=$info['code'];
		$infos['account']=$info['account'];
		$infos['username']=$info['name'];
		$infos['userid']=$this->userid;
		$infos['pay_type']=$type;
		$infos['status']=1;
		$infos['add_time']=time();
		$bind=$this->check_account($account);
		if ($bind){
				$data['flag']=0;
				$data['msg']="该账户已绑定！";
				echo json_encode($data);
		}else{
		$pay=$this->account_db->add($infos);
		if ($pay){
		$sms=$this->sms_db->where(array('userid'=>$this->userid,'id_code'=>$code))->find();
		if ($sms && $sms['enum']==$type){
				$data['flag']=1;
				$data['msg']=$msg;
				echo json_encode($data);
		}else{
			
				$data['flag']=0;
				$data['msg']="绑定失败！";
				echo json_encode($data);
		}
		
		}
		}
	
	}
	
	
	/*提现*/
	public function cash_bind($pay){
		$info=I('post.');
		$money=$info['value'];
		$account_id=$info['accountid'];
		$act=$info['act'];
		
		if($act !='cash'){
			return false;
		}
		$user=$this->db->where(array('userid'=>$this->userid))->find();
		$password = htmlspecialchars($info['pwd']);
		$config = getcache('setting', 'member');
		$u_coin=$money*$config['u_coin'];
		if ($money<$pay){
			$data['flag'] = 0;
			$data['msg'] ="对不起，单笔提现至少'$pay'元起";
			echo json_encode($data);
			return ;
		}
		if ($user['password']!=  md5(md5($password.$user['encrypt']))){
			$data['flag'] = 0;
			$data['msg'] ="密码输入错误！";
			echo json_encode($data);
			return ;
		}
		if ($u_coin>$user['u_coin']){
			$data['flag'] = 0;
			$data['msg'] ="当前的U币不足以提现";
			echo json_encode($data);
			return ;
		}else{	
		$account=$this->account_db->where(array('id'=>$account_id))->find();
		$info['payee']=$account['username'];
		$info['account']=$account['account'];
		$info['apply_money']=$money;
		$info['apply_coin']=$u_coin;
		$info['cash_time']=time();
		$info['userid']=$this->userid;
		$info['cash_type']=$account['pay_type'];
		$cash=$this->cash_db->add($info);
		if ($cash){
			$data['flag'] = 1;
			$data['msg'] ="提现成功,预计24小时到账！";
			echo json_encode($data);
		}
		}
	
	}
	
/**
	 * 检查账号是否存在
	 */
	public function check_account($account){
		$result=$this->account_db->where(array('account'=>$account))->find();
		return $result;
	}
	
	
/*提现记录*/
	public function cashList(){
		$SEO=seo(0,"我的提现信息记录");
		$pagecurr = max(1,I('page',0,'intval'));
		$pagesize = 10;
		$count=$this->cash_db->where(array('userid'=>$this->userid))->count();
		$cash=$this->cash_db->where(array('userid'=>$this->userid))->page($pagecurr,$pagesize)->order('cash_time DESC')->select();
		$pages = page($count,$pagesize);
		$money_sql="select sum(apply_money)as money from tp_cash where userid=".$this->userid;
		$total=$this->cash_db->query($money_sql);
		include template('cashlist');
	
	}
	
	
	/*判断电话是否绑定*/
	public function is_bind(){
		$result=$this->db->where(array('userid'=>$this->userid,'phone_status'=>1))->find();
		return $result;
	}
}